package com.gs.common.shiro;

/**
 * @title: 孙杨申
 * @date 2021/12/11 14:08
 */

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }

    //将自己的验证方式加入容器
    @Bean
    public CustomJWTRealm myShiroRealm() {
        CustomJWTRealm customJwtRealm = new CustomJWTRealm();
        return customJwtRealm;
    }

    //权限管理，配置主要是Realm的管理认证
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }

    //Filter工厂，设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filters = new LinkedHashMap<>();
        //设置我们自定义的JWT过滤器
        filters.put("jwt", new JWTFilter());
        shiroFilterFactoryBean.setFilters(filters);
        Map<String, String> filterMap = new LinkedHashMap<>();
        //登出
//        map.put("/logout", "logout");
//        //对所有用户认证
//        map.put("/**", "authc");
//        错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
        //VUE前端静态资源不校验，其中heyue原名是20060102是动态的，改为静态名称，要在config/index.jsp里设置
        filterMap.put("/index.html", "anon");
        filterMap.put("/assets/**", "anon");
        filterMap.put("/tinymce/**", "anon");
        filterMap.put("/**/login", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/api/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/doc.html", "anon");
        filterMap.put("/webjars/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        //根目录不过滤，用于页面的访问。根目录放开时并没有设通配符，所以子目录还是要权限
        filterMap.put("/disk/common/oss/download/**", "anon");
        // 壁纸
        filterMap.put("/wallper/mobile/**", "anon");

        // web页面
        filterMap.put("/disk/web/**", "anon");
        // 临时开启
//        filterMap.put("/disk/common/menu/**", "anon");

        // 所有请求通过我们自己的JWT Filter
//        filterMap.put("/**", "anon");
        filterMap.put("/**", "jwt");
        filterMap.put("/unauthorized", "anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        // 设置无权限时跳转的 url;
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
        return shiroFilterFactoryBean;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }
}